package DB;

import java.sql.*;

public class ConnectionDB {

        //Driver and database name
        private static final String driver = "jdbc:sqlserver://localhost:1433";
        private static final String databaseName = ";databaseName=WesternStyles";

        //Credentials
        private static String userName = ";user=sa";
        private static String password = ";password=masterkey";

        private DatabaseMetaData dma;
        private static Connection conn;
        private static ConnectionDB instance = null;

        //Private constructor
        private ConnectionDB() {

            String url = driver + databaseName + userName + password;

            //Trying to load the driver
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                System.out.println("Loading of driver - OK");
            }
            catch (Exception e) {
                System.out.println("Loading of driver - FAILED");
                System.out.println(e.getMessage());
            }

            try {
                conn = DriverManager.getConnection(url);
                conn.setAutoCommit(true);

                dma = conn.getMetaData();

                System.out.println("Connection to: " + dma.getURL());
                System.out.println("Driver: " + dma.getDriverName());
                System.out.println("Database product name: " + dma.getDatabaseProductName());

            } catch (Exception e) {
                System.out.println("Problem connecting to the database");
                System.out.println(e.getMessage());
                System.out.println(url);
            }
        }

        //Singleton pattern
        public static ConnectionDB getInstance() {
            if (instance == null) {
                instance = new ConnectionDB();
            }
            return instance;
        }

        //CloseDB
        public static void closeConnection() {
            try {
                conn.close();
                System.out.println("Connection is closed");
            }
            catch (Exception e) {
                System.out.println("Error trying to close the connection" + e.getMessage());
            }
        }

        //Get conn - returns the connection to the database
        public static Connection getConn() {
            return conn;
        }

        //Start transaction
        public static void startTransaction() {
            try {
                conn.setAutoCommit(false);
            }
            catch (Exception e) {
                System.out.println("Error starting transaction");
                System.out.println(e.getMessage());
            }
        }

        //Commit transaction
        public static void commitTransaction() {
            try{
                conn.setAutoCommit(true);
            }
            catch(Exception e){
                System.out.println("Error committing transaction");
                System.out.println(e.getMessage());
            }
        }

        //Rollback transaction
        public static void rollbackTransaction() {
            try{
                conn.rollback();
                conn.setAutoCommit(true);
            }
            catch(Exception e){
                System.out.println("fejl rollback transaction");
                System.out.println(e.getMessage());
            }
        }
}
